package logic

import (
	"context"
	"fmt"
	"zhihu/models/mysql"

	"zhihu/zhihurpc/internal/svc"
	"zhihu/zhihurpc/zhihurpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserPerfectLogic) UserPerfect(in *zhihurpc.UserPerfectRequest) (*zhihurpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.Users
	mysql.DB.Debug().Where("id = ?", in.UserId).Find(&findUser)
	if findUser.ID == 0 {
		return nil, fmt.Errorf("未找到该用户，请确认后重新输入")
	}
	users := mysql.Users{
		Sex:     in.Sex,
		Desc:    in.Desc,
		Address: in.Address,
		Work:    in.Work,
	}
	mysql.DB.Debug().Where("id = ?", in.UserId).Updates(&users)

	return &zhihurpc.UserPerfectResponse{
		UserId: int64(findUser.ID),
	}, nil
}
